package demo4;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-06
 * Time: 20:20
 */

//Comparable实现的接口有很大的缺陷 - 太嵌入
/*class Student implements Comparable<Student>{
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score){
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
    //谁调用这个方法谁就是this
    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;
        return this.name.compareTo(o.name);
    }
}*/

//对类的侵入性非常弱 - 一般推荐比较器
class Student {
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score){
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}
class AgeComparstor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class ScoreComparstor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o2.score - o1.score);
    }
}
class NameComparstor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main3(String[] args) {
        Student students1 = new Student(12,"彭国富",98.9);
        Student students2 = new Student(13,"刘瑞",99.9);

        //System.out.println(students1.compareTo(students2));
        AgeComparstor ageComparstor = new AgeComparstor();
        System.out.println(ageComparstor.compare(students1,students2));
    }
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(14,"张三",78.9);
        students[1] = new Student(13,"李四",86.9);
        students[2] = new Student(12,"王五",99.5);

        System.out.println(Arrays.toString(students));
        AgeComparstor ageComparstor = new AgeComparstor();
        ScoreComparstor scoreComparstor = new ScoreComparstor();
        NameComparstor nameComparstor = new NameComparstor();

        Arrays.sort(students,nameComparstor);

        System.out.println(Arrays.toString(students));
    }
    public static void main1(String[] args) {
        int[] array = {1,21,3,14,5,16};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(14,"张三",78.9);
        students[1] = new Student(13,"李四",86.9);
        students[2] = new Student(12,"王麻子",99.5);

        System.out.println(Arrays.toString(students));

        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
    }
}
